package cn.zshprint.serein.pojo;

import cn.zshprint.serein.exception.ServiceException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class ResultBean<T> implements Serializable {
    private static final Long serialVersionUID = 1L;
    public static final Integer SUCCESS = 0;
    public static final Integer FAIL = -1;
    public static final Integer NO_PERMISSION = 2;


    private String message = "success";
    private Integer code = SUCCESS;
    private T data;

    public ResultBean() {
        super();
    }

    public ResultBean(T data) {
        super();
        this.data = data;
    }



    public ResultBean(Throwable throwable) {
        super();
        if (throwable instanceof ServiceException) {
            ServiceException e = (ServiceException) throwable;
            this.code = e.getCode();
        } else {
            this.code = FAIL;
        }
        this.message = throwable.getMessage();
    }



    @Override
    public String toString() {
        return "ResultBean{" +
                "message='" + message + '\'' +
                ", code=" + code +
                ", data=" + data +
                '}';
    }
}